import redis
from redis import Redis

from helper.asynccall import asynccall


class CRedis(Redis):
    """Redis封装"""
    def __init__(self,
        host="localhost",
        port=6379,
        db=0,
        password=None,
        socket_timeout=None,
        socket_connect_timeout=None,
        socket_keepalive=None,
        socket_keepalive_options=None,
        connection_pool=None,
        unix_socket_path=None,
        encoding="utf-8",
        encoding_errors="strict",
        charset=None,
        errors=None,
        decode_responses=False,
        retry_on_timeout=False,
        retry_on_error=[],
        ssl=False,
        ssl_keyfile=None,
        ssl_certfile=None,
        ssl_cert_reqs="required",
        ssl_ca_certs=None,
        ssl_ca_path=None,
        ssl_check_hostname=False,
        ssl_password=None,
        ssl_validate_ocsp=False,
        ssl_validate_ocsp_stapled=False,
        ssl_ocsp_context=None,
        ssl_ocsp_expected_cert=None,
        max_connections=None,
        single_connection_client=False,
        health_check_interval=0,
        client_name=None,
        username=None,
        retry=None,
        redis_connect_func=None,call=None ):
        super().__init__(host=host, port=port, password=password, db=db, decode_responses=decode_responses)
        #pass
        self.handerMsg(call);
    @asynccall
    def handerMsg(self,call):
        r = redis.StrictRedis()
        pub = r.pubsub()  # Return a Publish/Subscribe object.
        pub.subscribe('__keyevent@0__:expired')

        for msg in pub.listen():
            print(msg['type'])
            print(msg['channel'])
            print(msg['data'])
            if (call!=None):
                call(msg);